package com.ruoyi.web.controller.system.app;

import java.util.Date;
import java.util.List;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.UtilTool;
import com.ruoyi.system.domain.*;
import com.ruoyi.system.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 推送Controller
 * 
 * @author ruoyi
 * @date 2021-07-30
 */
@Controller
@RequestMapping("/system/push")
public class AppPushController extends BaseController
{
    private String prefix = "system/push";

    @Autowired
    private IAppPushService appPushService;
    @Autowired
    private IWebSiteService webSiteService;
    @Autowired
    private IAppUserService appUserService;
    @Autowired
    private IAppUserPushService appUserPushService;
    @Autowired
    private IAppUserAboutService appUserAboutService;

    /**
     * 查询推送列表
     */
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(AppPush appPush)
    {
        checkPush(new AppPush());
        startPage();
        List<AppPush> list = appPushService.selectAppPushList(appPush);
        return getDataTable(list);
    }

    /**
     * 导出推送列表
     */
    @Log(title = "推送", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(AppPush appPush)
    {
        List<AppPush> list = appPushService.selectAppPushList(appPush);
        ExcelUtil<AppPush> util = new ExcelUtil<AppPush>(AppPush.class);
        return util.exportExcel(list, "推送数据");
    }

    /**
     * 新增保存推送
     */
    @Log(title = "推送", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(AppPush appPush)
    {
        // 获取当前的用户名称
        String username = SecurityUtils.getUsername();
        String[] ids = appPush.getSiteIds().split(",");
        appPush.setSiteNum(ids.length);
        appPush.setCreateSysUser(username);
        appPush.setCreateTime(new Date(System.currentTimeMillis()));
        appPush.setUpdateTime(new Date(System.currentTimeMillis()));
        return toAjax(appPushService.insertAppPush(appPush));
    }

    /**
     * 修改保存推送
     */
    @Log(title = "推送", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(AppPush appPush)
    {
        String[] ids = appPush.getSiteIds().split(",");
        appPush.setSiteNum(ids.length);
        appPush.setUpdateTime(new Date(System.currentTimeMillis()));
        return toAjax(appPushService.updateAppPush(appPush));
    }

    /**
     * 删除推送
     */
    @Log(title = "推送", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        String[] id = ids.split(",");
        for (int i=0;i<id.length;i++){
            AppUserPush appUserPush = new AppUserPush();
            appUserPush.setPushId(Long.valueOf(id[i]));
            appUserPushService.del(appUserPush);
        }
        return toAjax(appPushService.deleteAppPushByIds(ids));
    }

    /**
     * 查看推送详情
     */
    @PostMapping("/getDetailById")
    @ResponseBody
    public AjaxResult getDetailById(AppPush appPush)
    {
        appPush = appPushService.selectAppPushById(appPush.getId());
        if (UtilTool.isNull(appPush)){
            return AjaxResult.error("该数据为空");
        }else {
            List<WebSite> list = webSiteService.getWebSiteByIds(appPush.getSiteIds());
            return AjaxResult.success(list);
        }
    }

    //添加AppUserPush记录
    private void addAppUserPush(long pushId){
        List<AppUser> appUsers = appUserService.selectAppUserList(new AppUser());
        for (AppUser user:appUsers){
            AppUserAbout appUserAbout = new AppUserAbout();
            AppUserPush appUserPush = new AppUserPush();
            // type 0默认注册用户 1手机唯一码用户
            if (user.getType()==0){
                appUserPush.setUserName(user.getUserName());
                appUserAbout.setUserName(user.getUserName());
            }else {
                appUserPush.setUuid(user.getUuid());
                appUserPush.setUuid(user.getUuid());
            }
            appUserPush.setPushId(pushId);
            appUserPushService.insertAppUserPush(appUserPush);
            List<AppUserAbout> appUserAbouts = appUserAboutService.selectAppUserAboutList(appUserAbout);
            for (AppUserAbout about:appUserAbouts){
                appUserAboutService.deleteAppUserAboutById(about.getId());
            }
        }
    }

    /**
     * 查询是否有没有更新的通知
     * @param appPush
     */
    private void checkPush(AppPush appPush){
        appPush.setState(0);
        List<AppPush> list = appPushService.selectAppPushList(appPush);
        for (AppPush push:list){
            if (!UtilTool.isNull(push.getEffectTime())){
                if (push.getEffectTime().getTime()<System.currentTimeMillis()){
                    push.setState(1);
                    push.setUpdateTime(new Date(System.currentTimeMillis()));
                    appPushService.updateAppPush(push);
                    addAppUserPush(push.getId());
                }
            }
        }
    }
}
